//todo-list
//1. how to bind key ? //quitAct->setShortcuts(QKeySequence(tr("Ctrl+T")));	??
//2.

#include <QApplication>
#include <QMainWindow>
#include <QAction>
#include <QAction>
#include <QMenuBar>
#include <QWidget>
#include <QToolBar>
#include <QPlainTextEdit>
#include <QFileDialog>
#include <QDockWidget>
#include <QLabel>
#include <QTextEdit>
#include <QStatusBar>
#include <QGridLayout>

#include "MainWindow.h"


//for standard c++
#include <iostream>
using namespace std;
//cout << "hello" << endl;


MainWindow ::  MainWindow(QWidget *parent)
  : QMainWindow(parent)
{
  //QActions
  QAction *newAct= new QAction(tr("&New"), this);
  newAct->setShortcuts(QKeySequence::New);
  newAct->setStatusTip(tr("Create a new file"));
  connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
  //QAction *openAct = new QAction(QIcon(":/images/open.png"), tr("&Open"), this);		//where is the path of the images ??
  QAction *openAct = new QAction(QIcon("../images/open.png"), tr("&Open"), this);
  openAct->setShortcuts(QKeySequence::Open);
  openAct->setStatusTip(tr("open an existing file"));
  connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));
  QAction *quitAct = new QAction(tr("&Quit"), this);
  quitAct->setShortcuts(QKeySequence::Quit);
  quitAct->setStatusTip(tr("Quit"));
  connect(quitAct, SIGNAL(triggered()), this, SLOT(close()));
  /*  QAction *aboutAct = new QAction(tr("&About"), this);
  aboutAct->setStatusTip(tr("show the app's about box"));
  connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
  */
  QAction *aboutQtAct = new QAction(tr("&About"), this);
  aboutQtAct->setStatusTip(tr("show the Qt library's about box"));
  connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

  //for test
  QAction *testAct = new QAction(tr("&Test"), this);
  //  quitAct->setShortcuts(QKeySequence(tr("Ctrl+X, Ctrl+S")));
  //quitAct->setShortcuts(QKeySequence(tr("Ctrl+T")));	??
  quitAct->setStatusTip(tr("test"));
  connect(testAct, SIGNAL(triggered()), this, SLOT(test()));


  //disable or enable some actions
  newAct->setEnabled(false);


  //create menubar and add qactions
  QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
  fileMenu->addAction(newAct);
  fileMenu->addAction(openAct);
  fileMenu->addAction(quitAct);
  fileMenu->addSeparator();		//separator
  fileMenu->addAction(testAct);
  QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
  QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
  helpMenu->addAction(aboutQtAct);

  //create toolbar
  QToolBar *fileToolBar = addToolBar(tr("File"));
  fileToolBar->addAction(newAct);
  fileToolBar->addAction(openAct);
  fileToolBar->addAction(quitAct);
  fileToolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
  addToolBar(Qt::TopToolBarArea, fileToolBar);


  //
  /*  QLabel *label = new QLabel(tr("hello"));

  //create QDockWidget
  QDockWidget *dockWidget = new QDockWidget(tr("Dock Widget"), this);
  dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
  dockWidget->setWidget(label);
  addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
  */

  //create the centeral widget
  //QWidget *centralWidget = new QWidget(this);
  //setCentralWidget(centralWidget);
  //  textEdit = new QPlainTextEdit;
  //  setCentralWidget(textEdit);

  QLabel *nameLabel = new QLabel(tr("Name:"));
  nameLine = new QLineEdit;
  QLabel *contentLabel = new QLabel(tr("Content"));
  contentText = new QTextEdit;
  
  QGridLayout *mainLayout = new QGridLayout;
  mainLayout->addWidget(nameLabel, 0, 0);
  mainLayout->addWidget(nameLine, 0, 1);
  mainLayout->addWidget(contentLabel, 1, 0, Qt::AlignTop);
  mainLayout->addWidget(contentText, 1, 1);
  setLayout(mainLayout);


  //QMessageBox::about(this, ),

  //status bar ????
  statusBar()->showMessage(tr("ready"));
  //statusBar()->showMessage(tr("ready"), 2000);

  //window title
  //setWindowTitle(tr("Qt4 template"));
}

void MainWindow::openFile()
{
  QString fileName = QFileDialog::getOpenFileName(this);
  if(!fileName.isEmpty())
    cout << "not empty " << endl;


    
}

void MainWindow::newFile()
{
  QString fileName = QFileDialog::getOpenFileName(this);
  /*  if(!fileName.isEmpty())
    cout << "hello" << endl;
  */
}

void MainWindow::test()
{
  QString fileName = QFileDialog::getOpenFileName(this);
}



int main(int argc, char **argv)
{
  QApplication app(argc, argv);
  app.setOrganizationName("kang@xmu");
  app.setApplicationName("Qt4 Application Template");
  //app.setWindowIcon();
  //app.setMainWidget(&mw);
  MainWindow mw;
  mw.resize(640,480);
  mw.show();
  mw.setWindowTitle(QApplication::translate("toplevel", "top-level widget"));
  return app.exec();
}
